package com.yy.tech.inte.service.initializer;

import com.yy.tech.inte.service.wrapper.HttpRequestWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import javax.annotation.Resource;

/**
 * technical-identification$
 *
 * @author YY
 * @version 1.0.0
 * @date $ $Øø
 * @description: TODO
 */
@Component
public class HttpRequestInitializerFactory {

    @Autowired(required = false)
    private List<HttpRequestInitializer> initializers;

    @Autowired
    private RestTemplate restTemplate;

    @Resource(name = "inteRestTemplate")
    private RestTemplate inteRestTemplate;

    public <T,S> void initialize(HttpRequestWrapper<T,S> wrapper){
        if (!CollectionUtils.isEmpty(initializers)){
            initializers.stream().filter(i->i.support(wrapper)).findFirst().ifPresent(i->i.initialize(wrapper));
        }
    }

    public void setInteRestTemplate(final RestTemplate inteRestTemplate) {
        this.inteRestTemplate = inteRestTemplate;
    }

    public RestTemplate getRestTemplate(){
        if (null != inteRestTemplate){
            return inteRestTemplate;
        }
        return restTemplate;
    }
}
